﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraTabbedMdi;
using NjaNjam.MongoRepository;
using NjamNjamAdminApp.Interfaces;
using NjamNjamAdminApp.Properties;
using NjamNjamAdminApp.Views;

namespace NjamNjamAdminApp
{
    public partial class MainWindow : DevExpress.XtraBars.Ribbon.RibbonForm, IShell
    {
        private const string VrsteRestorana = "VrsteRestorana";

        private const string Restorani = "Restorani";

        private const string Pretraga = "Pretraga";

        private const string Ponuda = "Ponuda";

        private const string VrstePonude = "VrstePonude";

        private const string PretragaVrstePonude = "PretragaVrstePonude";

        private readonly HashSet<string> m_OpenedViews = new HashSet<string>();
        
        private readonly MongoRepository m_MongoRepository = new MongoRepository();

        public MainWindow()
        {
            InitializeComponent();
            btnNoviUnos.ItemClick += (s, e) => OnNoviUnos();
            btnOsvezi.ItemClick += (s, e) => OnOsvezi();
            btnSacuvaj.ItemClick += (s, e) => OnSacuvaj();
            btnObrisi.ItemClick += (s, e) => OnObrisi();
            btnDbDelete.ItemClick += (s, e) => OnObrisiBazu();
            btnIndex.ItemClick += (s, e) => OnIndeksirajBazu();
            btnExportSlika.ItemClick += (s, e) => OnIzveziSlike();
            
            

            btnVrsteRestorana.LinkClicked += (s, e) => OnVrsteRestoranaClicked();
            btnRestorani.LinkClicked += (s, e) => OnRestoraniClicked();
            btnPretraga.LinkClicked += (s, e) => OnPretragaClicked();
            btnPonuda.LinkClicked += (s, e) => OnPonudaClicked();
            btnVrstePonude.LinkClicked += (s, e) => OnVrstePonudeClicked();
            btnPretraziVrstuPonude.LinkClicked += (s, e) => OnPretraziVrstuPonude();
        }

        

        private void OnIzveziSlike()
        {
            if (ShowQuestion("Počni izvoz slika?"))
            {
                try
                {
                    StarLongRunningOperation("Izvoz u toku...");
                    var view = GetSelectedView();
                    if (view != null)
                    {
                        view.Izvezi();
                        alertControl1.Show(this, "Njam, Njam Info", "Izvoz slika završen.", Resources.info_balloon);
                    }
                }
                finally
                {
                    StopLongRunningOperation();
                }
            }
        }

        private void OnIndeksirajBazu()
        {
            if (ShowQuestion("Počni indeksiranje?"))
            {
                m_MongoRepository.Index();
                alertControl1.Show(this, "Njam, Njam Info", "Indeksiranje uspešno završeno.", Resources.info_balloon);
            }
        }

        private void OnObrisiBazu()
        {
            if (ShowQuestion("Da li ste sigurni da želite da obrišete bazu podataka?"))
            {
                m_MongoRepository.ObrisiBazu();
                alertControl1.Show(this, "Njam, Njam Info", "Brisanje baze završeno.", Resources.info_balloon);
            }
        }

        public bool ShowQuestion(string question)
        {
            return DialogResult.Yes ==  XtraMessageBox.Show(this, question, "Njam, Njam - Potvrda", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                MessageBoxDefaultButton.Button2);
        }

        private void OnVrstePonudeClicked()
        {
            if (m_OpenedViews.Add(VrstePonude))
            {
                var frm = new VrstaPonudeView() { MdiParent = this, Tag = VrstePonude };
                frm.Closed += OnViewClosed;
                frm.Show();
            }
            else
            {
                Aktiviraj(VrstePonude);
            }
        }

        private void OnPonudaClicked()
        {
            if (m_OpenedViews.Add(Ponuda))
            {
                var frm = new PonudaView() { MdiParent = this, Tag = Ponuda };
                frm.Closed += OnViewClosed;
                frm.Show();
            }
            else
            {
                Aktiviraj(Ponuda);
            }
        }

        private void OnPretragaClicked()
        {
            if (m_OpenedViews.Add(Pretraga))
            {
                var frm = new PretragaRestoranaView() { MdiParent = this, Tag = Pretraga };
                frm.Closed += OnViewClosed;
                frm.Show();
            } else
            {
                Aktiviraj(Pretraga);
            }
        }

        private void OnRestoraniClicked()
        {
            if (m_OpenedViews.Add(Restorani))
            {
                var frm = new RestoraniView() { MdiParent = this, Tag = Restorani };
                frm.Closed += OnViewClosed;
                frm.Show();
            }
            else
            {
                Aktiviraj(Restorani);
            }
        }

        private void OnVrsteRestoranaClicked()
        {
            if (m_OpenedViews.Add(VrsteRestorana))
            {
                var frm = new VrsteRestoranaView {MdiParent = this, Tag = VrsteRestorana};
                frm.Closed += OnViewClosed;
                frm.Show();    
            } else
            {
                Aktiviraj(VrsteRestorana);
            }
        }

        private void OnPretraziVrstuPonude()
        {
            if (m_OpenedViews.Add(PretragaVrstePonude))
            {
                var frm = new PretragaVrstePonude() { MdiParent = this, Tag = PretragaVrstePonude };
                frm.Closed += OnViewClosed;
                frm.Show();
            }
            else
            {
                Aktiviraj(PretragaVrstePonude);
            }
        }

        private void OnViewClosed(object sender, EventArgs e)
        {
            var view = (Form) sender;
            var tag = (string) view.Tag;
            m_OpenedViews.Remove(tag);
            view.Closed -= OnViewClosed;
        }

        private void Aktiviraj(string view)
        {
            foreach(XtraMdiTabPage page in xtraTabbedMdiManager1.Pages)
            {
                if ((string)page.MdiChild.Tag == view)
                {
                    xtraTabbedMdiManager1.SelectedPage = page;
                    break;
                }
            }
        }

        private void OnObrisi()
        {
            try
            {
                StarLongRunningOperation("Bisanje u toku...");
                var view = GetSelectedView();
                if (view != null)
                {
                    view.Obrisi();
                }
            }
            finally
            {
                StopLongRunningOperation();
            }
        }

        private void OnSacuvaj()
        {
            var view = GetSelectedView();
            if (view != null)
            {
                view.Sacuvaj();
                alertControl1.Show(this, "Njam, Njam Info", "Podaci su uspešno sačuvani", Resources.document_save__2_);
            }
        }

        private void OnOsvezi()
        {
            try
            {
                StarLongRunningOperation("Osvežavanje podataka u toku...");
                var view = GetSelectedView();
                if (view != null)
                {
                    view.Osvezi();
                }
            }
            finally
            {
                StopLongRunningOperation();
            }
        }

        private void OnNoviUnos()
        {
            var view = GetSelectedView();
            if (view != null)
            {
                view.NoviUnos();
            }
        }

        private IView GetSelectedView()
        {
            var page = xtraTabbedMdiManager1.SelectedPage;
            if (page != null)
            {
                return page.MdiChild as IView;
            }
            return null;
        }

        protected override void OnLoad(EventArgs e)
        {
            
        }

        public void StarLongRunningOperation(string message)
        {
            status.Caption = message;
            this.Cursor = Cursors.WaitCursor;
            status.Refresh();
        }

        public void StopLongRunningOperation()
        {
            status.Caption = null;
            this.Cursor = Cursors.Default;
            status.Refresh();
        }

        public void ShowWarning(string message)
        {
            XtraMessageBox.Show(this, message, "Njam,Najm Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}